<?php

namespace app\dlc\controller;

class AdminController extends BaseController
{
    //主页
    public function index(){
        return view();
    }

	/**
     * 后台管理员、角色添加
	 * @return [none] [description]
     */
	public function adminEdit(){
		$bread = array(
            '0' => array(
                'name' => '菜单设置',
                'url' => url('Dlc/role/authDetail')
            ),
            '1' => array(
                'name' => '菜单列表',
                'url' => url('Dlc/role/roleList')
            ),
            '2' => array(
                'name' => '帐号编辑',
                'url' => url('Dlc/admin/adminEdit')
            )
        );
        $this->assign('breadhtml', $this->getBread($bread));
		$id = input('id');
		$roleid = input('role_id');
		$m = model('admin');
        if(request()->isPost()){
			$data = input('post.');
            if (!$data['roleid']){
                $info['status'] = 0;
                $info['msg'] = '请选择类型！';
            }
            if (!empty($id)) {
                $find = $m->where(['username'=>$data['username'],'id'=>$id])->find();
                if($find){
                    $info['status'] = 0;
                    $info['msg']    = '此帐号已被注册过了';
                    return($info);
                }
                $re = $m->where('id',$id)->update($data);
                if (FALSE !== $re) {
                    $info['status'] = 1;
                    $info['msg'] = '设置成功！';
                } else {
                    $info['status'] = 0;
                    $info['msg'] = '设置失败1！';
                }
            } else {
                $find = $m->where('username',$data['username'])->find();
                if($find){
                    $info['status'] = 0;
                    $info['msg']    = '此帐号已被注册过了';
                    return($info);
                }
                $data['ctime'] = time();
                $data['password'] = md5($data['password']);
                $re = $m->insert($data);
                if (FALSE !== $re) {
                    $info['status'] = 1;
                    $info['msg'] = '设置成功！';
                } else {
                    $info['status'] = 0;
                    $info['msg'] = '设置失败！';
                }
            }
            return($info);
		}
		if (!empty($id)){
            $cache = $m->where('id = ' . $id)->find();
			$this->assign('cache', $cache);
        }
        $list = model('role')->select();
        $this->assign('list', $list);
        $this->assign('role_id',$roleid);
		echo $this->fetch('adminEdit');
	}


	/**
     * 后台管理员列表
	 * @return [none] [description]
    */
    public function adminList(){
        $bread = array(
            '0' => array(
                'name' => '菜单设置',
                'url' => url('Dlc/role/authDetail')
            ),
            '1' => array(
                'name' => '管理员列表',
                'url' => url('Dlc/admin/adminList')
            )
        );
        $p = input('page')?:1;
		$psize = 20;
        $this->assign('breadhtml', $this->getBread($bread));
        $list = db('admin')->order('id')->page($p,$psize)->select();
        foreach ($list as $key => $value) {
            $list[$key]['role_name'] = db('role')->where(['role_id'=>$value['roleid']])->value('role_name');
        }
        // dump($list);
		$count = db('admin')->count();
		$this->getPage($count, $psize, 'App-loader', '管理员列表', 'App-search');
        $this->assign('list',$list);
		echo $this->fetch('adminList');
    }

    //后台角色删除
    public function del()
    {
        $id = input('id');
        if(empty($id)){
            $info['status'] = 0;
            $info['msg']    = 'ID不能为空';
            return($info);
        }
        $re = model('admin')->where('id','in',$id)->delete();
        if($re !== false){
            $info['status'] = 1;
            $info['msg']    = '删除成功';
        }else{
            $info['status'] = 0;
            $info['msg']    = '删除失败';
        }
        return($info);
    }
}